import {
  YForm,
  YInterception,
  YItemConfig,
  YItemRow,
  YNodes,
  YNodesFunction,
} from '../interface';
import ItemConfig from './ItemConfig';
import ItemRow from './ItemRow';
import ItemRowList from './ItemRowList';
import ItemText from './ItemText';

type NodeType = ItemRow | ItemConfig | ItemRowList | ItemText;
type FormConfig = Array<
  YItemConfig | YItemRow | YItemRow[] | string | number | boolean
>;

export default class ItemConfigNode {
  data: NodeType;
  constructor(node: YNodes, public form: YForm) {
    if (Array.isArray(node)) {
      this.data = this.getItemRowLit(node);
    } else if ((node as YItemConfig).type) {
      this.data = new ItemConfig(node as YItemConfig, null, null, form);
    } else if ((node as YItemRow).children) {
      this.data = new ItemRow(node as YItemRow, null, null, form);
    } else {
      this.data = new ItemText(
        node as string | number | boolean | YNodesFunction,
        null,
        null,
        form
      );
    }
  }

  getItemRowLit(formConfig: FormConfig) {
    const rowList = new ItemRowList(null, null, this.form);
    formConfig.forEach((config) => rowList.add(config));
    return rowList;
  }

  getConfig(interception?: YInterception) {
    return this.data.getConfig(interception) || [];
  }

  setDisabled(disable: boolean, props?: string[]) {
    return this.data.setDisabled(disable, props);
  }

  setRequired(required: boolean, props?: string[]) {
    return this.data.setRequired(required, props);
  }

  setBind(props: string | string[], key: string, value: any) {
    return this.data.setBind(props, key, value);
  }

  setOptions(prop: string, options: { [key: string]: any }[]) {
    return this.data.setOptions(prop, options);
  }

  appendConfigBefore(prop: string, config: YItemConfig) {
    return this.data.appendConfigBefore(prop, config);
  }

  appendConfigAfter(prop: string, config: YItemConfig) {
    return this.data.appendConfigAfter(prop, config);
  }

  removeRow(id: string | number) {
    return this.data.removeRow(id);
  }

  appendRowBefore(
    id: string | number,
    config: YItemConfig | YItemRow | YItemRow[]
  ) {
    return this.data.appendRowBefore(id, config);
  }

  appendRowAfter(
    id: string | number,
    config: YItemConfig | YItemRow | YItemRow[]
  ) {
    return this.data.appendRowAfter(id, config);
  }

  getItemConfigs() {
    return this.data.getItemConfigs();
  }

  getRows() {
    return this.data.getRows();
  }
}
